<?php
namespace app\decorate\controller;
use app\AdminRest;
use think\App;
use think\Request;
use app\decorate\model\AdminCase as case_model;
class AdminCase extends AdminRest
{

    protected $model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new case_model();
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 16:10
     * @return \think\Response
     * descption:案例列表
     */
    public function caseList(){
        $input = $this->_param;
        if(!empty($input['title'])){
            $dis[] = ['title','like',"%\\".$input['title']."%"];
        }
        if(!empty($input['area'])){
            $dis[] = ['area','=',$input['area']];
        }
        if(!empty($input['designer'])){
            $dis[] = ['designer','=',$input['designer']];
        }
        $dis[] = ['uniacid','=',$this->_uniacid];
        $data = $this->model->caseList($dis,$input['limit']);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 16:11
     * @return \think\Response
     * descption:添加案列
     */
    public function caseAdd(){
        $input = $this->_input;
        $input = $this->model->changeData($input);
        $input['uniacid'] = $this->_uniacid;

        if (strlen($input['imgs']) > 6553) {
            return   $this->error('轮播图太多， 请减少轮播图数量');
        }

        $res = $this->model->caseAdd($input);
        return $this->success($res);
    }


    /**
     * User: chenniang
     * Date: 2019-10-09 16:11
     * @return \think\Response
     * descption:编辑|回显案列
     */
    public function caseUpdate(){
        $input = $this->_input;
        if(count($input)>1){
            $input = $this->model->changeData($input);
            $data  = $this->model->caseUpdate(['id'=>$input['id']],$input);
        }else{
            $data = $this->model->caseInfo(['id'=>$input['id']]);
        }
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-16 17:15
     * @return \think\Response
     * descption:上下架|删除
     */
    public function statusUpdate(){
        $input = $this->_input;
        $data  = $this->model->siteUpdate(['id'=>$input['id']],$input);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-16 17:15
     * @return \think\Response
     * descption:批量上下架|删除
     */
    public function statusUpdateAll(){
        $input = $this->_input;
        $dis[] = ['id','in',$input['id']];
        $data  = $this->model->caseUpdate($dis,['status'=>$input['status']]);
        return $this->success($data);
    }



    public function diylist()
    {
        $param = $this->_param;
        $filter[] = ['uniacid', '=', $this->_uniacid];
        $filter[] = ['status' , '=', 1];
        if (!empty($param['keyword'])) {
            $filter[] = ['title', 'like', '%' . $param['keyword'] . '%' ];
        }
        $list = $this->model->where($filter)
            ->paginate(['list_rows' => $param['page_count'] ? $param['page_count'] : 10, 'page' => $param['page'] ? $param['page'] : 1])->toArray();

        return $this->success($list);
    }

}
